//
//  KWNFeedbackViewController.m
//  KWNClient
//
//  Created by 张明磊 on 14/10/30.
//  Copyright (c) 2014年 张明磊. All rights reserved.
//

#import "KWLEditIntroViewController.h"
#import "KWNNetwork.h"
#import "RESideMenu.h"
#import "Model_user.h"
#import "KWLLoginViewController.h"

@interface KWLEditIntroViewController ()<UITextViewDelegate,UIAlertViewDelegate>

@property (weak, nonatomic) UITextView *textView_content;
@property (weak, nonatomic) UILabel *label_count;
@property (strong, nonatomic) NSString *string_content;

@property (assign, nonatomic) NSInteger int_tag;

@end

@implementation KWLEditIntroViewController

- (id)initWithTag:(NSInteger )int_tag andString:(NSString *)string_content{
    self = [super init];
    if(self){
        _int_tag = int_tag;
        _string_content = string_content;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self UI_feedback];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)UI_feedback{
    self.view.backgroundColor = RGB(227, 227, 227);
    
    UIView *imageView_title = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44 + IOS7_SIZE)];
    imageView_title.backgroundColor = RGB(91, 165, 229);
    [self.view addSubview:imageView_title];
    
    UIButton *button_menu = [UIButton buttonWithType:UIButtonTypeCustom];
    button_menu.frame = CGRectMake(0, IOS7_SIZE, 60, 45);
    [button_menu setImage:[UIImage imageFileName:@"button_back.png"] forState:UIControlStateNormal];
    [button_menu addTarget:self action:@selector(didClickButton_feedback:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_menu];
    button_menu.tag = 100;
    
    UIButton *button_login = [UIButton buttonWithType:UIButtonTypeCustom];
    button_login.frame = CGRectMake(250, IOS7_SIZE, 60, 45);
    [button_login setTitle:@"保存" forState:UIControlStateNormal];
    [button_login setTitleColor:ColorOfTitle forState:UIControlStateNormal];
    button_login.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
    [button_login addTarget:self action:@selector(didClickButton_feedback:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_login];
    button_login.tag = 101;
    
    UILabel *label_title = [[UILabel alloc] initWithFrame:CGRectMake(imageView_title.frame.origin.x,IOS7_SIZE + 5,imageView_title.frame.size.width,35)];
    label_title.backgroundColor = [UIColor clearColor];
    label_title.textColor = ColorOfTitle;
    label_title.textAlignment = NSTextAlignmentCenter;
    label_title.font = [UIFont fontWithName:@"Helvetica-Bold" size:16.0f];
    [self.view addSubview:label_title];
    if(_int_tag == 0){
        label_title.text = @"编辑个性签名";
    }
    else{
        label_title.text = @"编辑个人简介";
    }
    
    UIView *view_input = [[UIView alloc] initWithFrame:CGRectMake(0, imageView_title.frame.origin.y + imageView_title.frame.size.height,self.view.frame.size.width, self.view.frame.size.height - imageView_title.frame.size.height - imageView_title.frame.origin.y - Iphone5_size(278, 300))];
    view_input.backgroundColor = [UIColor clearColor];
    [self.view addSubview:view_input];
    
    UIView *view_board = [[UIView alloc] initWithFrame:CGRectMake(10, Iphone5_size(10, 20), view_input.frame.size.width - 20, view_input.frame.size.height - 20)];
    view_board.backgroundColor = [UIColor whiteColor];
    view_board.layer.cornerRadius = 6.0;
    view_board.layer.borderWidth = 1.0;//边框宽度
    view_board.layer.borderColor = RGB(197, 197, 197).CGColor;//边框颜色
    [view_input addSubview:view_board];
    
    UITextView *textView_input = [[UITextView alloc] initWithFrame:CGRectMake(20, view_board.frame.origin.y, view_board.frame.size.width - 20, view_board.frame.size.height - Iphone5_size(42, 55))];
    textView_input.backgroundColor = [UIColor clearColor];
//    textView_input.textColor = RGB(211, 211, 211);
    textView_input.textColor = RGB(109, 109, 109);
    textView_input.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    textView_input.autocorrectionType = UITextAutocorrectionTypeNo;
    textView_input.autocapitalizationType = UITextAutocapitalizationTypeNone;
    [view_input addSubview:textView_input];
    textView_input.delegate = self;
    textView_input.text = _string_content;
    _textView_content = textView_input;
    textView_input.tag = 200;
    [textView_input becomeFirstResponder];
    
    UIButton *button_closeKey = [UIButton buttonWithType:UIButtonTypeCustom];
    button_closeKey.frame = CGRectMake(0, 0, 320, 30);
    [button_closeKey setImage:[UIImage imageFileName:@"button_closeKey.png"] forState:UIControlStateNormal];
    [button_closeKey addTarget:self action:@selector(didClickButton_feedback:)forControlEvents:UIControlEventTouchUpInside];
    [textView_input setInputAccessoryView:button_closeKey];
    button_closeKey.tag = 102;
    
    UILabel *label_number = [[UILabel alloc] initWithFrame:CGRectMake(250, view_board.frame.origin.y + view_board.frame.size.height - 40, 60, 30)];
    label_number.backgroundColor = [UIColor clearColor];
    label_number.text = @"800字";
    label_number.textColor = RGB(209, 209, 209);
    label_number.textAlignment = NSTextAlignmentLeft;
    label_number.font = [UIFont fontWithName:@"Helvetica" size:13.0f];
    //[view_input addSubview:label_number];
    _label_count = label_number;
}

- (void)didClickButton_feedback:(UIButton *)button{
    [self.view endEditing:YES];
    switch (button.tag) {
        case 100:{
            [self popViewController];
        }break;
        case 101:{
            if(!_textView_content || _textView_content.text.length == 0){
                [SVProgressHUD showErrorWithStatus:@"内容不能为空" duration:1.5];
            }
            else{
                if(_int_tag == 0){
                    
                    if (_textView_content.text.length > 40) {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"字数过长，请不要超过40个字" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                        [alert show];
                        return;
                    }
                    else
                    {
                        [SVProgressHUD showWithStatus:@"发送中..." maskType:SVProgressHUDMaskTypeClear];
                        [[KWNNetwork sharedInstance] edit_laywerSign:_textView_content.text FinishBlock:^(NSInteger stateCode, NSMutableArray *result) {
                            switch (stateCode) {
                                case 10000:{
                                    NSString *string_message = result[0];
                                    [SVProgressHUD dismissWithSuccess:string_message afterDelay:1.5];
                                    double delayInSeconds = 1.5;
                                    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                                    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                                        [Model_user sharedInstance].bool_isLoadLawyer = YES;
                                        [self popViewController];
                                    });
                                }break;
                                case 10003:
                                {
                                    [SVProgressHUD dismiss];
                                    [self editIntroTokenIsFailure];
                                }break;
                                default:{
                                    NSString *string_message = result[0];
                                    [SVProgressHUD dismissWithError:string_message afterDelay:1.5];
                                }break;
                            }
                        }];
                    }
                }
                else{
// CS 快法务 KLW 2014-12-24
//                    [UIUtils view_showProgressHUD:@"发送中..." inView:self.view withType:0];
//                    
                    [SVProgressHUD showWithStatus:@"发送中..." maskType:SVProgressHUDMaskTypeClear];
// CE 快法务 KLW 2014-12-24
                    [[KWNNetwork sharedInstance] edit_laywerIntroduction:_textView_content.text FinishBlock:^(NSInteger stateCode, NSMutableArray *result) {
// DS 快法务 KLW 2014-12-24
//                        [UIUtils view_hideProgressHUDinView:self.view];
//                        
// DE 快法务 KLW 2014-12-24
                        switch (stateCode) {
                            case 10000:{
                                NSString *string_message = result[0];
// CS 快法务 KLW 2014-12-24
//                                [UIUtils view_showProgressHUD:string_message inView:self.view withTime:1.5f];
//                                
                                [SVProgressHUD dismissWithSuccess:string_message afterDelay:1.5];
// CE 快法务 KLW 2014-12-24
                                double delayInSeconds = 1.5;
                                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                                    [Model_user sharedInstance].bool_isLoadLawyer = YES;
                                    [self popViewController];
                                });
                            }break;
                            case 10003:
                            {
// AS 快法务 KLW 2014-12-24
                                [SVProgressHUD dismiss];
// AE 快法务 KLW 2014-12-24
                                [self editIntroTokenIsFailure];
//                                NSString *string_msg  = result[0];
//                                [UIUtils view_showProgressHUD:string_msg inView:self.view withTime:1.5f];
//                                double delayInSeconds = 1.5;
//                                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//                                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//                                    //code to be executed on the main queue after delay
//                                    KWLLoginViewController *login = [[KWLLoginViewController alloc] initWithTag:6];
//                                    [self pushViewController:login];
//                                });
                                
                            }
                                break;
                            default:{
                                NSString *string_message = result[0];
// CS 快法务 KLW 2014-12-24
//                                [UIUtils view_showProgressHUD:string_message inView:self.view withTime:1.5f];
//                                
                                [SVProgressHUD dismissWithError:string_message afterDelay:1.5];
// CE 快法务 KLW 2014-12-24
                            }break;
                        }
                    }];
                }
            }
        }break;
        case 102:{
            
        }break;
        default:
            break;
    }
}

#pragma mark - 账号被替换下线提示 -
- (void)editIntroTokenIsFailure{
    UIAlertView *alter_login;
    if(IOS8_VERSION){
        alter_login = [[UIAlertView alloc] initWithTitle:@"您的来问律师账号已在其它地方登录，请您重新登录此设备。如果不是您的操作，您的密码可能已泄露，请及时更改密码，或联系快问律师客服邮箱laiwenlvshi@kuaifawu.com。" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    }
    else{
        alter_login = [[UIAlertView alloc] initWithTitle:nil message:@"您的来问律师账号已在其它地方登录，请您重新登录此设备。如果不是您的操作，您的密码可能已泄露，请及时更改密码，或联系快问律师客服邮箱laiwenlvshi@kuaifawu.com。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    }
    alter_login.tag = 10003;
    [alter_login show];
}


#pragma mark - 输入代理 -
/**
 *  输入代理
 *
 *  @param textView 输入代理
 */
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//    if(text && textView.tag == 200){
//        textView.textColor = RGB(109, 109, 109);
//        textView.text = @"";
//        textView.tag = 201;
//    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView{
    _label_count.text = [NSString stringWithFormat:@"%lu字", (unsigned long)textView.text.length];
}

#pragma mark - 点击 -
/**
 *  点击
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:{
            [self popViewController];
        }break;
        case 1:{
            [self popViewController];
        }break;
            //账号替换
        case 10003:{
            KWLLoginViewController *login = [[KWLLoginViewController alloc] initWithTag:6];
            [self pushViewController:login];
        }break;
        default:
            break;
    }
}

@end
